class Solution:
    def highestPeak(self, isWater: list) -> list:
        def move(i, j):
            res = []
            if j - 1 >= 0:
                res.append((i, j - 1))
            if j + 1 < len(isWater[i]):
                res.append((i, j + 1))
            if i - 1 >= 0:
                res.append((i - 1, j))
            if i + 1 < len(isWater):
                res.append((i + 1, j))

            return res

        queue = []
        for i in range(len(isWater)):
            for j in range(len(isWater[i])):
                if isWater[i][j] == 1:
                    queue.append((i, j))
                    isWater[i][j] = 0
                else:
                    isWater[i][j] = 2**31
        level = 0
        while queue:
            level += 1
            for _ in range(len(queue)):
                i, j = queue.pop(0)
                for n, m in move(i, j):
                    if isWater[n][m] > level:
                        isWater[n][m] = level
                        queue.append((n, m))

        return isWater
